local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local base = require "base"
local tabletool = require "tabletool"
local timetool = require "timetool"
require "pdkenum"

local filename = "tabletimer.lua"

local TableTimer = {}

function TableTimer.doready(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]	
	if seat.rid ~= request.rid then
		return
	end
	if timerid ~= seat.ready_timer_id then
		return
	end
	seat.ready_timer_id = -1
	seat.ready_to_time = 0
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		-----判断朋友桌局数到没有
		if seat ~= nil then
			if table_data.cur_games_times < table_data.conf.game_times then
				-- seat.state = ESeatState.SEAT_STATE_WAIT_START
				-- server.logic.tablelogic.gameready(table_data, request, seat)
				return
			end
			server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
		end
	else
		if seat.rid > 0 then
			server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
		end
	end
end

function TableTimer.doaction(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	local seat = table_data.seats[request.seat_index]
	if seat then
		if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON
				or table_data.conf.room_type == ERoomType.ROOM_TYPE_SYSTEM then
			if seat.is_tuoguan == EBOOL.FALSE then
				seat.timeout_count = seat.timeout_count + 1
				if seat.timeout_count >= table_data.conf.action_timeout_count then
					seat.timeout_count = 0
					server.logic.seatlogic.dealtuoguan(table_data,seat)
				end
			end
		else
			return
		end
	else
		return
	end
	
	if server.logic.tablelogic.is_onegameend(table_data) or seat.rid ~= request.rid or table_data.action_type ~= request.action_type then
		return
	end
	table_data.timer_id = -1
	local status, cards
	if table_data.action_type == EActionType.ACTION_TYPE_CHUPAI then
		status, cards = server.logic.tablelogic.putcards(table_data)
		table_data.action_type = EActionType.ACTION_TYPE_CHUPAI
	elseif table_data.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
		if seat.is_tuoguan == EBOOL.TRUE then
			status, cards = server.logic.tablelogic.followcards(table_data, false, true)
			if status == true then
				table_data.action_type = EActionType.ACTION_TYPE_FOLLOW_CHUPAI
			else
				table_data.action_type = EActionType.ACTION_TYPE_CHECK
			end
		else
			table_data.action_type = EActionType.ACTION_TYPE_CHECK
		end
	end
	table_data.state = ETableState.TABLE_STATE_CONTINUE
	server.logic.gamelogic.run(table_data.gamelogic)
	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_SYSTEM then
		local action = timetool.get_time() .. "_" 
			.. seat.rid .. "_"
			.. (table_data.action_type or '0')
		server.logic.tablelogic.save_actions(table_data, action, cards)
	end
end

function TableTimer.delete_table(timerid, request)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.delete_table_timer_id == timerid then
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
				server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
			end
		end
		table_data.delete_table_timer_id = -1
        server.delete()
    end
end

function TableTimer.waitstartcountdown(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	if server.logic.tablelogic.is_onegameend(table_data) then
		return
	end
	server.logic.gamelogic.RiffleandPostCards(table_data)
end

----用于客户端展示牌局游戏结果界面定时器时间到
function TableTimer.onegameend(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	-- 切换房间状态到开启下一局游戏的状态
	table_data.state = ETableState.TABLE_STATE_ONE_GAME_END_AFTER
	server.logic.gamelogic.run(table_data.gamelogic)
end

function TableTimer.onegameendafter(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	--------切换房间状态到开启下一局游戏的状态
	table_data.state = ETableState.TABLE_STATE_ONE_GAME_REAL_END
	server.logic.gamelogic.run(table_data.gamelogic)
end

function TableTimer.disbandtable(timerid)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.vote_timerid ~= timerid then
		return
	end
	-- local noteresultntcmsg = {
	-- 	result = 0,
	-- 	names = table_data.vote_names,
	-- }
	local noteresultntcmsg = {
		table_index = table_data.conf.idx,
		is_success = false,
	}
	if not table_data.vote_num or table_data.vote_num == 0 then
		noteresultntcmsg.is_success = true
	end
	msghelper:sendmsg_to_alltableplayer("disbandresultN", noteresultntcmsg)
	table_data.vote_timerid = nil
	table_data.vote_rid = nil
	table_data.vote_name = nil
	table_data.vote_timeout = nil
	table_data.vote_num = nil
	table_data.vote_answers = nil
	-- table_data.vote_names = nil

	if noteresultntcmsg.is_success then
		if table_data.timer_id and table_data.timer_id > 0 then
			timer.cleartimer(table_data.timer_id)
			table_data.timer_id = -1
		end
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
				server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
			end
		end
		-- server.delete(table_data)
		table_data.delete_table_timer_id = timer.settimer(0, "delete_table")
	end
end

return TableTimer